﻿;############################## Empty Memory into Page File
EmptyMem(PID="AHK Rocks") {
    pid:=(pid="AHK Rocks") ? DllCall("GetCurrentProcessId") : pid
    h:=DllCall("OpenProcess", "UInt", 0x001F0FFF, "Int", 0, "Int", pid)
    DllCall("SetProcessWorkingSetSize", "Ptr", h, "Int", -1, "Int", -1)
    DllCall("CloseHandle", "Ptr", h)
  return
}

;convert name like Name23 to Name24
NameGetNext( Name, Count = 3 ) 
{
  if RegExMatch( Name, "\d{1," Count "}(?=\s*$)", num )
  {
    StringTrimRight, name, name, StrLen( num )
    return name . ++num
  }
  return name "1"
}

GetTimeFormat(pStandard,pFormat)
{
  FormatTime, fTime,% pStandard,% pFormat
  return fTime
}

RandColor()
{
  sleep 2
  return Rand(0,255) << 16 | Rand(0,255) << 8 | Rand(0,255)
}

Rand( num_start, num_end )
{
  Random, num,% num_start,% num_end
  return num
}

StrRemoveChars( str, chr)
{
  smb := strsplit(chr,"|",A_TAB A_Space)
  for i,char in smb
    StringReplace, str, str,% char,,1
  return str
}

QueryPerformanceCounter()
{
  DllCall( "QueryPerformanceCounter", "Ptr*", counter )
  return counter
}

/*
HLControl - function allow you to highlight specific ocntrols in window using graphical lines
  w_hwnd - HWND of the window where you like to draw lines
  hwnds - "|" delimeted list of controls hwnds which you like to highlight
  clr,clr2 - two colors for hoghlight

*/
HLControl(w_hwnd,hwnds,clr=0x0000FF,clr2=0xB73443)
{
  WinActivate,ahk_id %w_hwnd%
  Gui, +Disabled
  DC := DllCall("GetWindowDC","Ptr",w_hwnd,"UPtr") ;User32
  Pen := DllCall("CreatePen","Int",0,"Int",3,"UInt",clr,"UPtr")
  Pen2 := DllCall("CreatePen","Int",0,"Int",3,"UInt",clr2,"UPtr")
  obj_orig := DllCall("SelectObject","Ptr",DC,"Ptr",Pen,"UPtr")
  
    if !hModule := DllCall( "GetModuleHandleW", "Wstr", "Gdi32.dll", "UPtr" )
        hModule := DllCall( "LoadLibraryW", "Wstr", "Gdi32.dll", "UPtr" )
  MoveToP := DllCall("GetProcAddress", "Ptr", hModule, "Astr", "MoveToEx","UPtr")
  LineToP := DllCall("GetProcAddress", "Ptr", hModule, "Astr", "LineTo","UPtr")
  VarSetCapacity(pp,16,0)
  hwnd := StrSplit(hwnds,"|",A_Tab A_Space)
  loop, 12
  {
    sleep 10
    for i,hw in hwnd
    {
      ControlGetPos, X, Y, W, H,,% "ahk_id " hw
      DllCall(MoveToP,"Ptr",DC,"Int",X,"Int",Y,"Ptr",&pp)
      DllCall(LineToP,"Ptr",DC,"Int",X+W,"Int",Y)
      DllCall(LineToP,"Ptr",DC,"Int",X+W,"Int",Y+H)
      DllCall(LineToP,"Ptr",DC,"Int",X,"Int",Y+H)
      DllCall(LineToP,"Ptr",DC,"Int",X,"Int",Y)
    }
    sleep 100
    DllCall("SelectObject","Ptr",DC,"UInt",Mod(A_Index,2)=0 ? Pen2 : Pen)
  }
  WinSet,ReDraw,,ahk_id %w_hwnd%		;removing lines at this point
  DllCall("SelectObject","Ptr",DC,"Ptr",obj_orig)
  DllCall("DeleteObject","Ptr",Pen)
  DllCall("DeleteObject","Ptr",Pen2)
  DllCall("ReleaseDC","Ptr",w_hwnd,"Ptr",DC) ;User32

  Gui, -Disabled
  return
}

GetProcessList()
{
  d = `n  ; string separator
  s := 4096  ; size of buffers and arrays (4 KB)

  Process, Exist  ; sets ErrorLevel to the PID of this running script
  ; Get the handle of this script with PROCESS_QUERY_INFORMATION (0x0400)
  h := DllCall("OpenProcess", "UInt", 0x0400, "Int", false, "UInt", ErrorLevel, "Ptr")
  ; Open an adjustable access token with this process (TOKEN_ADJUST_PRIVILEGES = 32)
  DllCall("Advapi32.dll\OpenProcessToken", "Ptr", h, "UInt", 32, "PtrP", t)
  VarSetCapacity(ti, 16, 0)  ; structure of privileges
  NumPut(1, ti, 0, "UInt")  ; one entry in the privileges array...
  ; Retrieves the locally unique identifier of the debug privilege:
  DllCall("Advapi32.dll\LookupPrivilegeValue", "Ptr", 0, "Str", "SeDebugPrivilege", "Int64P", luid)
  NumPut(luid, ti, 4, "Int64")
  NumPut(2, ti, 12, "UInt")  ; enable this privilege: SE_PRIVILEGE_ENABLED = 2
  ; Update the privileges of this process with the new access token:
  r := DllCall("Advapi32.dll\AdjustTokenPrivileges", "Ptr", t, "Int", false, "Ptr", &ti, "UInt", 0, "Ptr", 0, "Ptr", 0)
  DllCall("CloseHandle", "Ptr", t)  ; close this access token handle to save memory
  DllCall("CloseHandle", "Ptr", h)  ; close this process handle to save memory

  hModule := DllCall("LoadLibraryW", "WStr", "Psapi.dll","UPtr")  ; increase performance by preloading the library
  s := VarSetCapacity(a, s)  ; an array that receives the list of process identifiers:
  c := 0  ; counter for process idendifiers
  DllCall("Psapi.dll\EnumProcesses", "Ptr", &a, "UInt", s, "UIntP", r)
  Loop, % r // 4  ; parse array for identifiers as DWORDs (32 bits):
  {
     id := NumGet(a, A_Index * 4, "UInt")
     ; Open process with: PROCESS_VM_READ (0x0010) | PROCESS_QUERY_INFORMATION (0x0400)
     h := DllCall("OpenProcess", "UInt", 0x0010 | 0x0400, "Int", false, "UInt", id, "Ptr")
     if !h
      continue
     VarSetCapacity(n, s, 0)  ; a buffer that receives the base name of the module:
     e := DllCall("Psapi.dll\GetModuleBaseName", "Ptr", h, "Ptr", 0, "Str", n, "UInt", A_IsUnicode ? s//2 : s)
     if !e    ; fall-back method for 64-bit processes when in 32-bit mode:
      if e := DllCall("Psapi.dll\GetProcessImageFileName", "Ptr", h, "Str", n, "UInt", A_IsUnicode ? s//2 : s)
       SplitPath n, n
     DllCall("CloseHandle", "Ptr", h)  ; close process handle to save memory
     if (n && e)  ; if image is not null add to list:
      l .= n . d, c++
  }
  DllCall("FreeLibrary", "Ptr", hModule)  ; unload the library to free memory
  ;Sort, l, C  ; uncomment this line to sort the list alphabetically
;~ 	MsgBox, 0, %c% Processes, %l%
  return, c . " Processes:`n`n" . l
}

IsUAC()
{
  RegRead,value,HKLM,Software\Microsoft\Windows\CurrentVersion\Policies\System,EnableLUA
  if ErrorLevel
    value := ErrorLevel
  return value
}

EvalMetaData( str )
{
  while RegExMatch( str, "iO)%(d|dl|t|ym|clip)([+\-]\d+)?%", match )
  {
    c_time := A_Now
    result := ""
    if( match[1] = "clip" )
      result := ClipsGetText( 0 )
    else
    {
      if match[2]
        c_time += match[2], M
      fmt := match[1] = "d" ? "ShortDate"
            : match[1] = "dl" ? "LongDate"
            : match[1] = "t" ? "Time"
            : match[1] = "ym" ? "YearMonth" : ShowExc( "Invalid time format: " match[1] )
      FormatTime, result,% c_time,% fmt
    }
    StringReplace,str,str,% match[0],% result,1
  }
  return str
}

SyntaxHelp()
{
  if A_Gui
    Gui, %A_Gui%:+OwnDialogs
  MsgBox,,Syntax Help,
  (LTrim
You can use following syntax to get dynamic date, time or clipboard values when clip or memo pasted.

`%d`%	  - Short date representation, such as 02/29/04
`%dl`%	  - Long date representation, such as Friday, April 23, 2004
`%t`%	  - Current time representation, such as 5:26 PM
`%ym`%	  - Year and month format, such as February, 2004
`%clip`%	  - The text data of system clipboard
  )
}

RGBtoBGR( bgr_clr )
{
  return ( bgr_clr & 0xFF0000 ) >> 16 | ( bgr_clr & 0x00FF00 ) | ( bgr_clr & 0x0000FF ) << 16
}

StringUpper( string )
{
  StringUpper, out_string, string
  return out_string
}

StringLower( string )
{
  StringLower, out_string, string
  return out_string
}

StringTitle( string )
{
  StringUpper, out_string, string, T
  return out_string
}

StringSentence( string )
{
  return RegExReplace( StringLower( string ), "`a)(\.\s*.|^\s*.|`n\s*.)", "$U0")
}

CallStack( offset = -1, deepness = 5, printLines = 1 )
{
  if A_IsCompiled
    return
  ident := "  "
  cnt := 0
  loop,% deepness
  {
    lvl := offset - deepness + A_Index
    oEx := Exception( "", lvl )
    codeLine := Trim( FileReadLine( oEx.file, oEx.line ), "`t " )
    if(oEx.What = lvl)
      continue
    oExPrev := Exception( "", lvl - 1 )
    cnt++
    stack .= ( cnt = 1 ? "" : "`n--`n" )
                  . "File '" oEx.file "', Line " oEx.line ( oExPrev.What < 0 ? "" : ", in " oExPrev.What )
                  . ( printLines ? ":`n" ident codeLine : "" )
  }
  return stack
}

ShowExc( exc )
{
  if isObject( exc )
    exc := "Error: " exc.message ( exc.Extra ? "`nExtra: " exc.Extra : "" ) "`n"
  exc .= "`n`n" CallStack( -2 )
  ans := QMsgBoxP( { title : "Exception info", msg : exc, pic : "!", editbox : 1, editbox_w : 500, editbox_h : 300, "buttons" : "Continue|Skip|Report|Quit App", rsz : 1 } )
  if ( "Quit App" = ans )
    ExitApp
  else if ( "Report" = ans )
  {
    fb_msg =
    ( LTrim
    Please let us know when does this error happens:
    
    
    ==== Error Text
    %exc%
    ==============
    )
    SendFeedback( fb_msg )
  }
  else if ( "Continue" != ans )
    Exit
  return 0
}

IsDirModified( sPath,ByRef oDirs = "" )
{
  res := object()
  loop,% PathAppend( sPath, "*" ), 2, 1
    res[ A_LoopFileLongPath ] := A_LoopFileTimeModified
  bResl := 0
  if oDirs
  {
    for path,time in res
      if ( oDirs[ path ] != time )
      {
        bResl := 1
        break
      }
  }
  else
    bResl := 1
  oDirs := res
  return bResl
}

